package cn.fsw.pethome.pet.controller;

import cn.fsw.pethome.loginutil.LoginContextUtils;
import cn.fsw.pethome.order.mapper.OrderPetAcquisitionMapper;
import cn.fsw.pethome.org.domain.Employee;
import cn.fsw.pethome.pet.domain.SearchMasterMsg;
import cn.fsw.pethome.pet.domain.dto.MasterMsgDTO;
import cn.fsw.pethome.pet.query.SearchMasterMsgQuery;
import cn.fsw.pethome.pet.service.ISearchMasterMsgService;
import cn.fsw.pethome.user.domain.User;
import cn.fsw.pethome.util.AjaxResult;
import cn.fsw.pethome.util.BusinessException;
import cn.fsw.pethome.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private LoginContextUtils loginContextUtils;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;


    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else{
                User loginUser =   (User)loginContextUtils.getLoginUser(request);
                searchMasterMsgService.add(searchMasterMsg,loginUser);
            }
            return AjaxResult.ajaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setSuccess(false).setMsg("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.delete(id);
            return AjaxResult.ajaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error().setSuccess(false).setMsg("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.selectAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.pageSelevtAll(query);
    }

    // 待处理
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        Employee employee = (Employee) loginContextUtils.getLoginUser(request);

        return searchMasterMsgService.pageSelevtAll(query,employee);
    }

    /*接单*/
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody  MasterMsgDTO dto){
        try {
            searchMasterMsgService.accept(dto);
            return AjaxResult.ajaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /*拒单*/
    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody  MasterMsgDTO dto){
        try {
            searchMasterMsgService.reject(dto);
            return AjaxResult.ajaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
}
